//
//  SmallDecisionsDpViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/25.
//

#import "SmallDecisionsDpViewController.h"
#import "SmallDecisionsScrollLabel.h"

@interface SmallDecisionsDpViewController ()

@property (nonatomic, strong) SmallDecisionsScrollLabel *scrollLabel;

@end

@implementation SmallDecisionsDpViewController

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.scrollLabel.minWidth = self.view.width;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];

    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self setSmallDecisionsDpConfiger];
    [self createSmallDecisionsDpCloseButton];
}

- (void)setSmallDecisionsDpConfiger {
    self.view.backgroundColor = self.dpBgColor;
    [self.scrollLabel setText:self.dpText refreshLabels:YES];
    self.scrollLabel.font = SmallDecisionsSetFontSize(self.dpFontSize);
    self.scrollLabel.textColor = self.dpFontColor;
    self.scrollLabel.scrollSpeed = self.dpSpeed;
    [self.scrollLabel refreshLabels];
}

- (void)createSmallDecisionsDpCloseButton{
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"global_close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeBtn];
    closeBtn.layer.cornerRadius = 15;
    closeBtn.layer.masksToBounds = YES;
    closeBtn.backgroundColor = [UIColor redColor];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(30);
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(20);
    }];
}


- (void)closeBtnAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (SmallDecisionsScrollLabel *)scrollLabel {
    if(!_scrollLabel) {
        _scrollLabel = [[SmallDecisionsScrollLabel alloc] init];
        _scrollLabel.textColor     = [UIColor redColor];
        _scrollLabel.font          = SmallDecisionsSetFontSize(100);
        _scrollLabel.labelSpacing  = 100;
        _scrollLabel.pauseInterval = 1.5;
        _scrollLabel.scrollSpeed   = 200;
        _scrollLabel.textAlignment = NSTextAlignmentLeft;
        _scrollLabel.fadeLength    = 12.f;
        _scrollLabel.text          = @"请输入文字";
        _scrollLabel.scrollDirection = SmallDecisionsAutoScrollDirectionLeft;
        [_scrollLabel observeApplicationNotifications];
        [self.view addSubview:_scrollLabel];
        [_scrollLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo([UIApplication sharedApplication].statusBarFrame.size.height);
            make.right.mas_equalTo(-[UIApplication sharedApplication].statusBarFrame.size.height);
            make.top.bottom.mas_equalTo(0);
        }];
    }

    return _scrollLabel;
}

- (BOOL)setHideNavBar {
    return YES;
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}


@end
